package cn.kgc.biz.controller;

import cn.kgc.biz.service.HotelOrderService;
import cn.kgc.biz.vo.AddHotelOrderVo;
import cn.kgc.biz.vo.HotelOrederVo;
import cn.kgc.biz.vo.PersonalOrderVo;
import cn.kgc.biz.vo.PreOrderInfoVo;
import cn.kgc.dto.Dto;
import cn.kgc.mapper.HotelOrderMapper;
import cn.kgc.pojo.HotelOrder;
import cn.kgc.pojo.User;
import cn.kgc.util.DtoUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/hotelorder/")
public class HotelOrderController {
    @Resource
    private HotelOrderService hotelOrderService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @PostMapping(value = "getpersonalorderlist",headers = "token")
    public Dto getPersonalOrderList(HttpServletRequest request, @RequestBody PersonalOrderVo personalOrderVo){
        String token=request.getHeader("token");
        if (!stringRedisTemplate.hasKey(token)){
            return DtoUtil.getFalseDto("用户未登录","200001");
        }
        User user=  JSON.parseObject(stringRedisTemplate.opsForValue().get(token),User.class);
        personalOrderVo.setUserId(user.getId());
        return hotelOrderService.getPersonalOrderList(personalOrderVo);
    }

//    @PostMapping(value = "addhotelorder",headers = "token")
//    public Dto addhotelorder(HttpServletRequest request,@RequestBody HotelOrederVo hotelOrederVo){
//        String token=request.getHeader("token");
//        if (!stringRedisTemplate.hasKey(token)){
//            return DtoUtil.getFalseDto("用户未登录","200001");
//        }
//        return hotelOrderService.addhotelorder(hotelOrederVo);
//    }
    @PostMapping(value = "getpreorderinfo",headers = "token")
    public Dto getPreOrderInfo(HttpServletRequest request, @RequestBody PreOrderInfoVo preOrderInfoVo){
        String token=request.getHeader("token");
        if (!stringRedisTemplate.hasKey(token)){
            return DtoUtil.getFalseDto("用户未登录","200001");
        }
        return hotelOrderService.getPreOrderInfo(preOrderInfoVo);
    }
    @GetMapping(value = "getpersonalorderinfo/{orderId}",headers = "token")
    public Dto getPersonalOrderInfo(@PathVariable("orderId") String orderId,HttpServletRequest request){
        String token=request.getHeader("token");
        if (!stringRedisTemplate.hasKey(token)){
            return DtoUtil.getFalseDto("用户未登录","200001");
        }
        return hotelOrderService.getPersonalOrderInfo(orderId);
    }
    @GetMapping("getpersonalorderroominfo/{orderId}")
    public Dto getPersonalOrderRoomInfo(@PathVariable("orderId") String orderId,HttpServletRequest request){
        String token=request.getHeader("token");
        if (!stringRedisTemplate.hasKey(token)){
            return DtoUtil.getFalseDto("用户未登录","200001");
        }
        return hotelOrderService.getPersonalOrderRoomInfo(orderId);
    }
    @PostMapping(value = "addhotelorder",headers = "token")
    public Dto addHotelOrder(@RequestBody AddHotelOrderVo addHotelOrderVo, HttpServletRequest request){
        String token=request.getHeader("token");
        if (!stringRedisTemplate.hasKey(token)){
            return DtoUtil.getFalseDto("用户未登录","200001");
        }
        if (token.startsWith("token:pc")){
           addHotelOrderVo.setBookType(0);
        }else if (token.startsWith("token:mobile")){
            addHotelOrderVo.setBookType(1);
        }else {
            addHotelOrderVo.setBookType(2);
        }
        User user = JSON.parseObject(stringRedisTemplate.opsForValue().get(token),User.class) ;
        return hotelOrderService.addHotelOrder(user,addHotelOrderVo);
    }
    @GetMapping(value = "queryOrderById/{orderId}",headers = "token")
    public Dto queryOrderById(@PathVariable("orderId") String orderId,HttpServletRequest request){
        String token=request.getHeader("token");
        if (!stringRedisTemplate.hasKey(token)){
            return DtoUtil.getFalseDto("用户未登录","200001");
        }
        return hotelOrderService.queryOrderById(Long.parseLong(orderId));
    }
}
